package parser;

import lexer.Token;
import lexer.TokenType;

/**
 * class, extending from exception, represents a ParserException. This class is merely used by
 * {@link parser.Parser}. It allows customised exception for syntax errors during parsing
 * process.
 * @author sebastian
 */
public class ParserException extends Exception {

	/**
	 * Default serial number
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * General constructor of a ParserException. ParserExceptions with special message can be
	 * created.
	 * @param msg the message the exception will print on command line, when it is thrown
	 */
	public ParserException(String msg) {
		super(msg);
	}
	
	/**
	 * Creates a new ParserException with a message that tells where in the programme (which Token)
	 * an unexpected Token was found and which other Tokens were Tokens that could be expected.
	 * @param found the Token that was not expected
	 * @param tokenTypes the TokenTypes that could be expected
	 * @throws ParserException a ParserException with an helpful message
	 */
	public static ParserException UnexpectedToken(Token found, TokenType ... tokenTypes ) throws ParserException {
		String msg = "";
		msg += String.format("Found this Token: %s at (%d, %d), but expected one of the following TokenTypes: ",found.toString(), found.getLine(), found.getCol());
		for (TokenType t : tokenTypes) {
			msg += String.format("\n%s",t.toString());
		}
		throw new ParserException(msg);
	}
	
}
